package com.imooc.framework.thread;

import java.util.concurrent.Semaphore;

public class SemaphoreDemo implements Runnable{

    private final Semaphore semaphore = new Semaphore(3);
    @Override
    public void run() {
        try {
            semaphore.acquire();
            Thread.sleep(5000);
            System.out.println(Thread.currentThread().getName());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            semaphore.release();
        }
    }

    public static void main(String[] args) {
        SemaphoreDemo semaphoreDemo = new SemaphoreDemo();

        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(semaphoreDemo);
            t.setName("thread" + i);
            t.start();
        }
    }
}
